package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huike.entity.*;
import com.huike.mapper.*;
import com.huike.vo.*;
import com.huike.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl implements IndexService {
    //线索
    @Autowired
    private ClueMapper clueMapper;
    //商机
    @Autowired
    private BusinessMapper businessMapper;
    //客户
    @Autowired
    private CustomerMapper customerMapper;
    //合同金额
    @Autowired
    private ContractMapper contractMapper;
    // 员工
    @Autowired
    private SysUserMapper sysUserMapper;
    //部门
    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public TopSalesVo topSales(LocalDate begin, LocalDate end) {
        TopSalesVo topSalesVo = new TopSalesVo();
        List<ClueDragonAndTigerVO> clueList = new ArrayList<>();
        List<BusinessDragonAndTigerVO> businessList = new ArrayList<>();
        // 线索排行榜
        List<SysUser> sysUsers = sysUserMapper.selectList(null);

        for (SysUser sysUser : sysUsers) {
            ClueDragonAndTigerVO clueDragonAndTigerVO = new ClueDragonAndTigerVO();
            BusinessDragonAndTigerVO businessDragonAndTigerVO = new BusinessDragonAndTigerVO();
            String userName = sysUser.getUserName();
            // 线索排行榜员工名称
            clueDragonAndTigerVO.setName(userName);
            businessDragonAndTigerVO.setName(userName);

            Integer clueCount = clueMapper.selectCount(new LambdaQueryWrapper<Clue>()
                    .between(begin != null && end != null, Clue::getCreateTime, begin, end)
                    .eq(Clue::getBelonger, userName)
            );
            Integer businessCount = businessMapper.selectCount(new LambdaQueryWrapper<Customer>()
                    .between(begin != null && end != null, Customer::getCreateTime, begin, end)
                    .eq(Customer::getBelonger, userName)
            );
            // 线索排行榜线索数量
            clueDragonAndTigerVO.setClueCount(clueCount);
            businessDragonAndTigerVO.setBusinessCount(businessCount);

            // 获取部门
            Department department = departmentMapper.selectOne(new LambdaQueryWrapper<Department>()
                    .eq(Department::getId, sysUser.getDeptId()));
            String deptName = department.getDeptName();
            clueDragonAndTigerVO.setDept(deptName);
            businessDragonAndTigerVO.setDept(deptName);

            // 转换率
            Integer clueCountOk = clueMapper.selectCount(new LambdaQueryWrapper<Clue>()
                    .between(begin != null && end != null, Clue::getCreateTime, begin, end)
                    .eq(Clue::getBelonger, userName)
                    .eq(Clue::getIsPool, 0)
            );
            if (clueCount != 0) {
                Integer i = clueCountOk / clueCount * 100;
                clueDragonAndTigerVO.setCluePercentage(i);
                clueList.add(clueDragonAndTigerVO);
            }
            businessDragonAndTigerVO.setBusinessPercentage(100);
            businessList.add(businessDragonAndTigerVO);


        }
        List<ClueDragonAndTigerVO> sortedList = clueList.stream()
                .sorted(Comparator.comparingInt(ClueDragonAndTigerVO::getClueCount).reversed())
                .limit(10)
                .collect(Collectors.toList());
        topSalesVo.setClueDragonAndTigerVOList(sortedList);

        List<BusinessDragonAndTigerVO> sortedList1 = businessList.stream()
                .sorted(Comparator.comparingInt(BusinessDragonAndTigerVO::getBusinessCount).reversed())
                .limit(10)
                .collect(Collectors.toList());
        topSalesVo.setBusinessDragonAndTigerVOList(sortedList1);
        return topSalesVo;
    }


    /**
     * 待办事项
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BacklogVo backlog(LocalDate begin, LocalDate end) {
        BacklogVo backlogVo = new BacklogVo();
        // 待跟进商机
        Integer WaitBusiness = customerMapper.selectCount(new LambdaQueryWrapper<Customer>()
                .between(begin != null && end != null, Customer::getCreateTime, begin, end)
                .eq(Customer::getStatus, "待跟进"));
        backlogVo.setWaitBusiness(WaitBusiness);
        // 待分配商机
        Integer waitAssignBusiness = customerMapper.selectCount(new LambdaQueryWrapper<Customer>()
                .between(begin != null && end != null, Customer::getCreateTime, begin, end)
                .eq(Customer::getStatus, "待分配"));
        backlogVo.setWaitAssignBusiness(waitAssignBusiness);
        // 待跟进线索
        Integer waitLeads = clueMapper.selectCount(new LambdaQueryWrapper<Clue>()
                .between(begin != null && end != null, Clue::getCreateTime, begin, end)
                .eq(Clue::getStatus, "1"));
        backlogVo.setWaitLeads(waitLeads);
        // 待分配线索
        Integer waitAssignLeads = clueMapper.selectCount(new LambdaQueryWrapper<Clue>()
                .between(begin != null && end != null, Clue::getCreateTime, begin, end)
                .eq(Clue::getStatus, "0"));
        backlogVo.setWaitAssignLeads(waitAssignLeads);
        return backlogVo;
    }

    /**
     * 查询基本数据
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BasicDataVo basicData(LocalDate begin, LocalDate end) {

        // 创建对象
        BasicDataVo basicDataVo = new BasicDataVo();
        // 根据begin和end时间查询线索池中的线索数量
        Integer clue = clueMapper.selectCount(new LambdaQueryWrapper<Clue>()
                .between(begin != null && end != null, Clue::getCreateTime, begin, end));
        basicDataVo.setNumberOfLeads(clue);
        // 根据begin和end时间查询商机跟进的商机数量
        Integer business = businessMapper.selectCount(new LambdaQueryWrapper<Customer>()
                .between(begin != null && end != null, Customer::getCreateTime, begin, end));
        basicDataVo.setNumberOfBusinessOpportunities(business);
        // 根据begin和end时间查询公海池的客户数量
        Integer customer = customerMapper.selectCount(new LambdaQueryWrapper<Customer>()
                .between(begin != null && end != null, Customer::getCreateTime, begin, end));
        basicDataVo.setNumberOfCustomers(customer);
        // 根据begin和end时间查询销售金额
        List<Contract> contracts = contractMapper.selectList(new LambdaQueryWrapper<Contract>()
                .between(begin != null && end != null, Contract::getCreateTime, begin, end));
        Double sum = 0.0;
        for (Contract contract : contracts) {
            Double contractOrder = 1.0 * contract.getContractOrder();
            sum += contractOrder;
        }
        basicDataVo.setSalesAmount(sum);
        return basicDataVo;
    }

    /**
     * 今日数据
     *
     * @return
     */
    @Override
    public BasicDataVo todayBulletin() {
        LocalDate now = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);

        // 创建对象
        BasicDataVo basicDataVo = new BasicDataVo();
        // 根据begin和end时间查询线索池中的线索数量
        Integer clue = clueMapper.selectCount(new LambdaQueryWrapper<Clue>()
                .between(begin != null && end != null, Clue::getCreateTime, begin, end));
        basicDataVo.setNumberOfLeads(clue);
        // 根据begin和end时间查询商机跟进的商机数量
        Integer business = businessMapper.selectCount(new LambdaQueryWrapper<Customer>()
                .between(begin != null && end != null, Customer::getCreateTime, begin, end));
        basicDataVo.setNumberOfBusinessOpportunities(business);
        // 根据begin和end时间查询公海池的客户数量
        Integer customer = customerMapper.selectCount(new LambdaQueryWrapper<Customer>()
                .between(begin != null && end != null, Customer::getCreateTime, begin, end));
        basicDataVo.setNumberOfCustomers(customer);
        // 根据begin和end时间查询销售金额
        List<Contract> contracts = contractMapper.selectList(new LambdaQueryWrapper<Contract>()
                .between(begin != null && end != null, Contract::getCreateTime, begin, end));
        Double sum = 0.0;
        for (Contract contract : contracts) {
            Double contractOrder = 1.0 * contract.getContractOrder();
            sum += contractOrder;
        }
        basicDataVo.setSalesAmount(sum);
        return basicDataVo;
    }

    /**
     * 漏斗
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesFunnelVo salesFunnel(LocalDate begin, LocalDate end) {
        SalesFunnelVo salesFunnelVo = new SalesFunnelVo();
        // 线索总数
        Integer clue = clueMapper.selectCount(new LambdaQueryWrapper<Clue>()
                .between(begin != null && end != null, Clue::getCreateTime, begin, end));
        return null;
    }
}
